import { Component, Size, size, _decorator } from "cc";
import VirtualList from "../../../../../app-core/common/component/virtuallist/VirtualList";
import { ItemArgs } from "./ListItemComp";
const { ccclass, property } = _decorator;

function calcItemSize(args: ItemArgs): Size {
    return size(0, args.num);
}

@ccclass
export default class LoginComp extends Component {
    @property(VirtualList) list: VirtualList<ItemArgs> = null;

    private _rnd: number = 0;
    private _heights: number[] = [80, 100, 180];

    onLoad() {
        this.list.setCalcItemSize(calcItemSize);

        // for (let i = 0; i < 10; i++) {
        //     this.list.push({ num: Tool.arrayRand(this._heights) });
        // }

        // this._rnd = Tool.randInt(0, this.list.argsArr.length);
    }

    addItem() {
        this.list.push({ num: Tool.arrayRand(this._heights) });
    }

    clearItem() {
        this.list.splice(0, this.list.argsArr.length);

        // this.addItem();
        // this.addItem();
    }

}

class Tool {
    public static arrayRand<T>(arr: Array<T>): T {
        if (arr.length <= 0) {
            return null;
        }
        return arr[this.randInt(0, arr.length)];
    }

    public static randInt(min: number, max: number = undefined): number {
        if (max === undefined) {
            max = min;
            min = 0;
        }
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min;
    }

    public static randFloat(min: number, max: number = undefined): number {
        if (max === undefined) {
            max = min;
            min = 0;
        }
        return Math.random() * (max - min) + min;
    }
}